﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CinemabookingWCFApp.SeatsFolder
{

    public class SeatsService : ISeatsService
    {
        #region
        CinemabookingDBDataContext db = new CinemabookingDBDataContext();
        #endregion

        public List<TheaterSeat> GetAllSeats()
        {
            var queryResult = (from x in db.TheaterSeats
                               select x);
            return queryResult.ToList();
        }

        public List<TheaterSeat> GetAllRows(int Row)
        {
            var queryResult = (from x in db.TheaterSeats
                               select x);
            return queryResult.ToList();
        }

        public List<TheaterSeat> GetSeat(int SeatId)
        {
            var queryResult = (from x in db.TheaterSeats
                               where x.id == SeatId
                               select x);
            return queryResult.ToList();
        }

        public List<TheaterSeat> GetAllSeatsInRow(int Row)
        {
            var queryResult = (from x in db.TheaterSeats
                               where x.rowId == Row
                               select x);
            return queryResult.ToList();
        }

        public List<TheaterSeat> GetAllSeatsInTheater(int TheaterId)
        {
            var queryResult = (from x in db.TheaterSeats
                               where x.theaterId == TheaterId
                               select x);
            return queryResult.ToList();
        }

        public List<TheaterSeat> GetRowsOnRunTime(int RunTime)
        {
            var queryResult = (from x in db.TheaterSeats
                               where x.runTime == RunTime
                               select x);
            return queryResult.ToList();
        }

        public List<TheaterSeat> GetSeatsOnRowAndRunTime(int RowId, int RunTime)
        {
            var queryResult = (from x in db.TheaterSeats
                               where x.rowId == RowId
                               where x.runTime == RunTime
                               where x.available == true
                               select x);
            return queryResult.ToList();
        }

        public void UpdateAvalibility(int SeatId)
        {
            TheaterSeat updSeat = (from x in db.TheaterSeats
                                   where x.id == SeatId
                                   select x).Single();

            updSeat.available = false;
            db.SubmitChanges();
        }

        public void SetSeatsToAvalible(string PhoneNr)
        {
            List<BookedSeat> lst = new List<BookedSeat>();

            var queryResult = (from x in db.BookedSeats
                               where x.bookingInfo == PhoneNr
                               select x);

            lst.AddRange(queryResult);
            
            foreach (var seat in lst.Where(s => s.bookingInfo == PhoneNr))
            {
                seat.TheaterSeat.available = true;
            }

            db.SubmitChanges();
        }

        public void FindBestSeats(int RowId, int RunTime)
        {
            
        }
    }
}
